package servlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
 
	private static final String Appid = "wxae23616ec8ea0459"; //科泰学堂 固定的
    private static final String Secret = "12d731aa9478dde9a0163ae54db2fbc8";//密匙 固定的
    private static final String REDIRECT_URL 
    = "http://hdghpm.natappfree.cc/wechatLogin/callBackLogin"; //你扫码之后访问的url
    //hdghpm.natappfree.cc 改成你自己的内网穿透的url,要记得去微信开放平台把 Appid对应的授权回调域改掉
    //wechatLogin 你自己的项目名称
    //callBackLogin 你的servlet url

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//复制粘贴即可
            StringBuffer url = new StringBuffer("https://open.weixin.qq.com/connect/qrconnect?");
            url.append("appid=").append(Appid);
            url.append("&redirect_uri=").append(URLEncoder.encode(REDIRECT_URL, "UTF-8"));
            url.append("&response_type=code");
            url.append("&scope=").append("snsapi_login");
            url.append("&state=123#wechat_redirect");
            //重定向
            // requsest.getRe....("index.jsp").forward(request,response);
            response.sendRedirect(url.toString());//发送一个请求 url,浏览器会接收到一个微信的二维码
        } catch (Exception e) {
            e.printStackTrace();
        }
	
	}
	 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
